<script>
	import {
		SystemController
	} from '@/api/index.js';
	import {
		encrypt
	} from '@/utils'
	import appDialog from './utils/dialog.js';
	export default {
		onLaunch: function() {
			console.log('App Launch')
			//监测应用自动更新

			this.changLoginStatus()

		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			changLoginStatus() {
				let that = this
				let loginInfoFromFile = null;
				this.$filePersistentIO.read("RM_USR_INFO.txt").then((res) => {
					loginInfoFromFile = res;
					if (loginInfoFromFile.loginName && loginInfoFromFile.password) {
						// 缓存中存在登录用户则自动执行登录流程
						// that.login(loginInfoFromFile)

						SystemController.loginMobile(this, {
							loginName: loginInfoFromFile.loginName,
							password: encrypt(loginInfoFromFile.password)
						}).then((res) => {
							uni.setStorage({
								key: 'setHomeEntryList',
								data: res.mobileEntryList,
								success: function(e) {
									// let obj = {
									// 	loginName:obj.loginName,
									// 	password: encrypt(that.formData.password)
									// }
									// // 登录成功后将用户名密码存储到文件
									// try {
									// 	that.$filePersistentIO.storage("RM_USR_INFO.txt", obj);
									// } catch (error) {
									// 	// 浏览器会失效
									// }
									uni.setStorage({
										key: 'token',
										data: res.tokenData,
										success: function() {
											uni.switchTab({
												url: '/pages/work/index'
											})
										}
									});

									uni.setStorage({
										key: 'userId',
										data: res.userId
									});
									uni.setStorage({
										key: 'userInfo',
										data: res
									});
								}
							})
						}).catch((e) => {
							console.log(e)
						})
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";

	page {
		background-color: #F6F7F9;
		// padding: 32rpx;
	}

	uni-page-body {
		padding: 0;
	}
</style>